package com.jy.api.domain;

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.Date;

/**
 * Created by hanxi on 2017/7/22.
 */
@Entity
@Data
@NoArgsConstructor
public class Payment {
    @Id
    @Column(length = 50)
    private String id;
    private String orderId;
    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;
    private String storeId;
    private Double amount;
    private boolean success = false;
    private String channel;
    @Enumerated(EnumType.STRING)
    private PaymentStatus status;

    //当支付方式实物优惠券时，以下属性不为空
    /**
     * 优惠券名称
     */
    private String couponName;
    /**
     * 抵用商品id
     */
    private String productId;
    /**
     * 抵用商品名称
     */
    private String productName;
    /**
     * 优惠券实例编码
     */
    private String couponInstanceCode;
    /**
     * 抵用商品销售价格
     */
    private Integer productSalePrice;
    /**
     * 抵用商品数量
     */
    private Short productQuantity;

    private boolean enabled = true;

    /**
     * 优惠券类型
     */
    @Enumerated(EnumType.STRING)
    private CouponType type;

    @Column(insertable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date addTime;
    @Column(insertable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;

    public Payment(String id, String orderId, Double amount, PaymentChannel channel, String storeId) {
        this.id = id;
        this.orderId = orderId;
        this.createTime = new Date();
        this.success = false;
        this.amount = amount;
        this.channel = channel.name();
        this.storeId = storeId;
        this.status = PaymentStatus.NOTSURE;
    }

    public Payment(String id, String orderId, Double amount, String storeId, Date createTime) {
        this.id = id;
        this.orderId = orderId;
        this.createTime = createTime;
        this.success = true;
        this.amount = amount;
        this.channel = PaymentChannel.CASH.name();
        this.storeId = storeId;
        this.status = PaymentStatus.SUCCESS;

    }

    public boolean isThirdParty() {
        if (channel.startsWith("pay")) {
            return true;
        } else {
            return false;
        }
    }
}
